package top.i89ck.common.tree;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import top.i89ck.modules.utils.Const;

import cn.hutool.core.util.StrUtil;

/**
 * 系统中常用树表结构的数据的生成方法类
 *
 * @author Seiya
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public final class TreeUtils {

    /**
     * 获取普通的树形结构数据的方法
     *
     * @param models
     * @return
     */
    public static <M> List<M> getTreeList(List<? extends TreeModel> models) {
        List<TreeModel> roots = getRoot(models);
        setChild(roots, models);
        return (List<M>) roots;
    }

    /**
     * 获取最顶级的根节点数据
     *
     * @param models
     * @return
     */
    private static List<TreeModel> getRoot(List<? extends TreeModel> models) {
        List<TreeModel> roots = new ArrayList<>();
        for (TreeModel model : models) {
            if (Objects.isNull(model.getParentId()) || Const.DEFAULT_PARENT_ID.equals(model.getParentId())) {
                roots.add(model);
            }
        }
        return roots;
    }

    /**
     * 给根节点递归添加子类节点
     *
     * @param roots
     * @param models
     */
    private static void setChild(Collection<TreeModel> roots, List<? extends TreeModel> models) {
        for (TreeModel root : roots) {
            for (TreeModel model : models) {
                if (root.getId().equals(model.getParentId())) {
                    root.addChild(model);
                    setChild(root.getChildren(), models);
                }
            }
        }
    }

    /**
     * 获取页面树形展示的数据结构
     *
     * @param jqTrees
     * @return
     */
    public static <M> List<M> getJqTreeList(List<? extends JqTreeModel> jqTrees) {
        List<JqTreeModel> roots = getTreeList(jqTrees);
        List<JqTreeModel> list = new ArrayList<>();
        setJqTreeList(roots, 1, list);
        return (List<M>) list;
    }

    /**
     * 设置页面展示用的数据结构信息
     *
     * @param roots
     * @param level
     * @param list
     */
    private static void setJqTreeList(Collection<JqTreeModel> roots, int level, List<JqTreeModel> list) {
        int levelIndex = 1;
        for (JqTreeModel root : roots) {
            Set<JqTreeModel> children = root.getChildren();
            root.setLevel(level);
            root.setIsLeaf(root.getChildren().isEmpty());
            root.setExpanded(!root.getChildren().isEmpty());
            root.clearChildren();
            root.setLevelIndex(levelIndex);
            list.add(root);
            if (!children.isEmpty()) {
                setJqTreeList(children, level + 1, list);
            }
            levelIndex++;
        }
    }

    /**
     * 获取树结构的select控件数据解构
     *
     * @param treeModels
     */
    public static List<Map<String, Object>> getSelectTreeList(List<? extends TreeModel> treeModels) {
        List<TreeModel> roots = getTreeList(treeModels);
        List<Map<String, Object>> list = new ArrayList<>();
        setSelectTreeList(roots, list);
        return list;
    }

    /**
     * 设置select树结构的数据信息
     *
     * @param treeModels
     * @param list
     */
    private static void setSelectTreeList(Collection<TreeModel> treeModels, List<Map<String, Object>> list) {
        int i = 0;
        for (TreeModel treeModel : treeModels) {
            Map<String, Object> map = new HashMap<String, Object>(10);
            map.put("id", treeModel.getId() + "");
            map.put("name", treeModel.getName());
            map.put("open", i == 0);
            if (CollUtil.isNotEmpty(treeModel.getChildren())) {
                List<Map<String, Object>> children = new ArrayList<>();
                map.put("children", children);
                setSelectTreeList(treeModel.getChildren(), (List<Map<String, Object>>) map.get("children"));
            }
            list.add(map);
            i++;
        }
    }
}
